<?php

namespace Modules\ShopAdmin\Models;
use Illuminate\Support\Facades\Cache;
class AuthArea extends BaseApiModel
{
	/**
	 * @name  获取省市区
	 * @return json 
	 */
    public function get_area(){
		$area_list = Cache::get('area_list');
		if(!$area_list){
			$area_list = $this->select('id','pid','name')->orderBy('sort','asc')->orderBy('id','asc')->get()->toArray();		
			$area_list = $this->auth_area_arr($area_list);
			Cache::put('area_list',$area_list);
		}
		return $area_list;	
	}
	/**
	 * @name  处理省市区数据
	 * @param  list  array 省市区数据
	 * @return json 
	 */
	public function auth_area_arr($list){
		$arr = [];
		$i = 0;
		foreach($list as $k=>$v){
			if($v['pid'] == 0){
				$arr[$i] = $v;
				unset($list[$k]);
				$j = 0;
				foreach($list as $k1=>$v1){
					if($arr[$i]['id'] == $v1['pid']){
						$arr[$i]['children'][$j] = $v1;
						unset($list[$k1]);
						$s = 0;
						foreach($list as $k2=>$v2){
							if($arr[$i]['children'][$j]['id'] == $v2['pid']){
								$arr[$i]['children'][$j]['children'][$s] = $v2;
								unset($list[$k2]);
								$s++;
							}
							
						}
						$j++;
					}
				}
				$i++;
			}
		}
		return $arr;
	}
}
